<?php

namespace app\admin\controller;
use app\BaseController;
use think\facade\Db;


header('Content-Type:application/json; charset=utf-8');

class Login extends BaseController {


  public function login() {

    $request = request()->param();

    if (empty($request['username']) || empty($request['password'])){
      returnJson(-2, '用户名或密码错误！');
    }

    $adminModel = Db::table('sys_admin')->where('username', $request['username'])->find();
    if (empty($adminModel)) {
      returnJson(-2, '用户名或密码错误！');
    }
    if ($adminModel['password'] != md5($request['password'])){
      exit(json_encode(['code'=>-2, 'msg'=>'用户名或密码错误！']));
    }else{
      $info = [
        'id'=>$adminModel['id'], 
        'username'=>$adminModel['username'], 
        'nickname'=>$adminModel['nickname']
      ];
      session('adminUser', json_encode($info));
      // 不能使用exit die 会导致Session无效（TP6 BUG）
      echo(json_encode(['code'=>0, 'msg'=>'登陆成功！', 'data'=>$info]));
    }

  }

  public function out_login() {
    session('adminUser', null);
  }

}



